package smtp;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;

/**
 * the class holds a new email, it is kind of like the mimeMessage, but it's more easy to setup and can cache most exceptions
 * 
 * @author liweihan
 *
 */
public class NewEmail {
	public static final String IMAP="IMAP";
	public static final String IMAPS="IMAPS";
	public static final String POP3="POP3";
	public static final String POP3S="POP3S";
	//user session
	Session session = null;
	// hold this email 
	MimeMessage mail = null;
	// hold text content
	MimeBodyPart text = null;
	// hold attachments
	MimeBodyPart attachments= null;
	//error string is used to report error instead of throwing  exception, which make caller class ugly
	private static String error="";
	/**
	 * session is required to get user info and send this Email
	 * @param session
	 */
	public NewEmail(Session session){
		if(session==null){
			throw new IllegalArgumentException("can't be null session");
		}
		this.session=session;
		mail=new MimeMessage(session);		

	}
	/**
	 * set the subject
	 * @param subject
	 * @return true if succeed, error will write into the error String if false
	 */
	public boolean setSubject(String subject){
		boolean pass=true;
		try {
			mail.setSubject(subject);
		} catch (MessagingException e) {
			pass=false;
			error="Can't set subject, MessagingException";
		}
		return pass;
	}
	/**
	 * set the sendTo addresses
	 * @param from sender address
	 * @param to send to 
	 * @param cc send carbon copy
	 * @param bcc send blind carbon copy
	 * @return
	 */
	public boolean setAddresses(InternetAddress from,InternetAddress[] to,InternetAddress[] cc,InternetAddress[] bcc){
		boolean pass=true;
		try {
			if(from==null&&to==null){
				throw new IllegalArgumentException("FROM & TO is requried field");
			}
			mail.setFrom(from);
			mail.setRecipients(RecipientType.TO, to);
			if(cc!=null){
				mail.setRecipients(RecipientType.CC, cc);				
			}
			if(bcc!=null){
				mail.setRecipients(RecipientType.BCC, bcc);				
			}
		} catch (MessagingException e) {
			pass=false;
			error="MessagingException on setting addresses";
		}
		return pass;
	}
	/**
	 * set the attachment of this email
	 * @param files attachments
	 */
	public void setAttachments(File[] files){
		MimeMultipart allFiles = new MimeMultipart();
		MimeBodyPart oneFile=null;
		attachments = new MimeBodyPart();
		try {
			for(File file: files){
				oneFile = new MimeBodyPart();
				FileDataSource fds = new FileDataSource(file);  
				// oneFile.attachFile(file);
				oneFile.setDataHandler(new DataHandler(fds)); 
				oneFile.setFileName(file.getName());
				allFiles.addBodyPart(oneFile);
				//oneFile.setHeader(name, value)
			}
			attachments.setContent(allFiles);
			attachments.setDisposition(BodyPart.ATTACHMENT);
			

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	/**
	 * set the text content of this email
	 * @param body content body
	 * @param charset 
	 * @return true if succeed, error will write into the error String if false
	 */
	public boolean setText(String body,String charset){
		boolean pass=true;
		text= new MimeBodyPart();
		try {
			text.setText(body, charset);
			text.setDisposition(BodyPart.INLINE);
		} catch (MessagingException e) {
			pass=false;
			error="Can't set email content text, MessagingException";
		}
		return pass;

	}
	/**
	 * send email
	 * 
	 * @param protocol protocol used to send, usually "SMTP"
	 * @return true if succeed, error will write into the error String if false
	 */
	public boolean send(String protocol){
		boolean pass=true;
		MimeMultipart content = new MimeMultipart(); 
		try {
			mail.setSentDate(new Date());
		} catch (MessagingException e1) {
			pass=false;
			error="can't set Date";
		}
		try {
			if(text!=null){
				content.addBodyPart(text);
			}
			if(attachments!=null){

				content.addBodyPart(attachments);
			}
			mail.setContent(content);

		} catch (MessagingException e) {
			pass=false;
			error="Can't gather mail content";
		}

		try {
			Transport trans = session.getTransport(protocol);
			trans.connect(session.getProperties().getProperty("user"), session.getProperties().getProperty("pass"));
			System.out.println(session.getProperties().getProperty("user"));
			trans.sendMessage(mail, mail.getAllRecipients());
		} catch (NoSuchProviderException e) {
			pass=false;
			e.printStackTrace();
			error="Can't send Email, NoSuchProviderException";
		} catch (MessagingException e) {
			e.printStackTrace();
			pass=false;
			error="Can't send Email, MessagingException";
		}
		return pass;

	}
	/**
	 * get current, call it when a setter return false
	 * @return error string
	 */
	public String getError(){
		return error;
	}




}
